package com.example.learn.service;

import com.example.learn.domain.IncomeChart;
import com.example.learn.entity.Account;
import com.example.learn.entity.Expenditure;
import com.example.learn.entity.Income;
import com.example.learn.entity.User;
import com.example.learn.mapper.AccountMapper;
import com.example.learn.mapper.ExpenditureMapper;
import com.example.learn.mapper.IncomeMapper;
import com.example.learn.utile.TimeUtil;
import com.example.learn.utile.charutile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ExpenditureService {
    @Resource
    ExpenditureMapper expenditureMapper;
    @Resource
    AccountMapper accountMapper;


    public List<Expenditure> getexpenditure(String queryTime,String queryname, User user) {

        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        String queryTimeStart = TimeUtil.getfirstDay(queryTime);
        String queryTimeEnd = TimeUtil.getlastDay(queryTime);
        List<Expenditure> incomeList=    expenditureMapper.getByName(queryTimeStart,queryTimeEnd,names,queryname);
        return incomeList;


    }

    public String addexpenditure(Expenditure expenditure, User user) {
        if(StringUtils.isEmpty(expenditure.getId())){
            expenditure.setId(UUID.randomUUID().toString());
            expenditure.setName(user.getName());
            expenditure.setDisable(true);
            expenditure.setDate(TimeUtil.getCurrentTime());
            int i=      expenditureMapper.insert(expenditure);
        }else {
            expenditure.setName(user.getName());
            expenditure.setDisable(true);
            expenditure.setDate(TimeUtil.getCurrentTime());
            expenditureMapper.updateByPrimaryKey(expenditure);
        }

        return "ok";
    }

    public String deleteexpenditure(String id) {
        int i=  expenditureMapper.deleteByPrimaryKey(id);
        return  "ok";
    }

    public List<IncomeChart> topleftmethod(String queryTime, String queryname, User user) {
        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        String queryTimeStart = TimeUtil.getfirstDay(queryTime);
        String queryTimeEnd = TimeUtil.getlastDay(queryTime);
        List<Expenditure> incomeList = expenditureMapper.getByName( queryTimeStart, queryTimeEnd,names, queryname);
        Map<String, Double> map = new HashMap<>();
        List<IncomeChart> optionalerList = new ArrayList<>();
        for (Expenditure icome : incomeList) {
            double value = 0;
            if (map.keySet().contains(icome.getConsumeraddress())) {
                value = map.get(icome.getConsumeraddress()) + icome.getMoney();
                map.put(icome.getConsumeraddress(), value);
            } else {
                value = icome.getMoney();
                map.put(icome.getConsumeraddress(), value);
            }

        }
        map.forEach((key, value) -> {
            IncomeChart optionaler = new IncomeChart();
            optionaler.setValue(value);
            optionaler.setName(key);
            optionalerList.add(optionaler);
        });
        return optionalerList;
    }

    public List<String> downrightmethod(User user, String queryname, String queryTime) {
        Set<String> names = new HashSet<String>();
        if (queryname.isEmpty()) {
            queryname = "family";
        }
        if (queryname.equals("family")) {
            //先找出家庭人员
            List<Account> accounts = accountMapper.getFamilyName(user.getName());

            for (Account account : accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        } else {
            names.add(queryname);
        }
        List<Expenditure> incomeList = expenditureMapper.getByName2(names, "", "", queryname);
        String year = queryTime.substring(0,4);

        List<String> stringList = charutile.getcharYear2(incomeList,year);

        return stringList;
    }
}
